package com.qf.service.impl;

import com.qf.dao.OrderMapper;
import com.qf.dao.UserMapper;
import com.qf.pojo.Order;
import com.qf.pojo.OrderView;
import com.qf.pojo.User;
import com.qf.service.OrderService;
import com.qf.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    //添加订单
    @Override
    public BaseResult addOrder(Order order) {
        BaseResult baseResult = new BaseResult();
        if(order==null){
            baseResult.setCode(10001);
            return baseResult;
        }
        orderMapper.insertOrder(order);
        baseResult.setCode(1);
        baseResult.setInfo("订单提交成功!");
        return baseResult;
    }

    //查找当前用户的订单
    @Override
    public BaseResult findMyOrder(HttpSession session) {
        BaseResult baseResult = new BaseResult();
        User user = (User)session.getAttribute("user");
        List<Order> myOrder = orderMapper.findMyOrder(user);
        System.out.println("serviceImpl findMyOrder myOrder:"+myOrder);
        if (myOrder==null){
            baseResult.setCode(20000);
            return baseResult;
        }
        baseResult.setCode(1);
        baseResult.setInfo(myOrder);
        return baseResult;
    }

    //展示用户订单
    @Override
    public BaseResult viewMyOrder(HttpSession session) {
        BaseResult baseResult = new BaseResult();
        User user = (User) session.getAttribute("user");
        List<OrderView> orderViews = orderMapper.viewOrder(user);
        if(orderViews==null){
            baseResult.setCode(20000);
            baseResult.setData("该用户没有订单!");
            return baseResult;
        }
        baseResult.setCode(1);
        baseResult.setInfo(orderViews);
        return baseResult;
    }

    //删除订单
    @Override
    public BaseResult delMyOrder(int oid) {
        BaseResult baseResult = new BaseResult();
        orderMapper.delOrder(oid);
        Order orderById = orderMapper.findOrderById(oid);
        //没有删除成功
        if(orderById!=null){
            baseResult.setCode(10000);
            baseResult.setInfo("删除失败!");
            return baseResult;
        }
        baseResult.setCode(1);
        baseResult.setInfo("删除成功!");
        return baseResult;
    }

    @Override
    public BaseResult findAll() {
        List<Order> all = orderMapper.findAll();
        BaseResult baseResult = new BaseResult();
        baseResult.setCode(0);
        baseResult.setData(all);
        return baseResult;
    }

}
